package com.laot.search.datasource;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.laot.seacher_master.model.datasource.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;


@Component
@Slf4j
public class DataSourceRegistry {


    @Autowired
    private ApplicationContext applicationContext;
    private Map<String, DataSource> typeDataSourceMap;

    @PostConstruct
    public void init(){
        //拿到实现DataSource接口的所有类
        typeDataSourceMap = applicationContext.getBeansOfType(DataSource.class);
        log.info("加载完成的map：{}", JSON.toJSONString(typeDataSourceMap));
    }

    public Map<String,DataSource> getDataSourceMap(){
        return this.typeDataSourceMap;
    }

    public DataSource getDataSource(String type){
        if(type==null){
            return null;
        }
        return typeDataSourceMap.get(type);
    }

}
